package com.jade.bss.customer.permission;

import java.io.Serializable;
import java.security.Permission;
import java.util.Date;
import javax.persistence.*;
import com.jade.framework.base.lang.ReflectUtils;
import com.jade.framework.base.security.ActionPermission;
/**
 * 权限实体对象。权限为Principal能够对系统所做的操作或者能够获取某项资源的能力。
 *
 * @author sky
 * @version 1.0 13-4-18 上午9:13
 */
@Entity
@Table (name = "BSS_CUSTOMER_PERMISSION")
public class PermissionEntry implements Serializable
{
    /** 权限id */
    @Id
    @GeneratedValue (generator = "BSS_SEQ_PERMISSION")
    @SequenceGenerator (name = "BSS_SEQ_PERMISSION", sequenceName = "BSS_SEQ_PERMISSION")
    private int id;
    /** 权限所属组标识 */
    @Column (name = "group_id")
    private int groupId;
    //客户所有者标识
    @Column (name = "owner_id", nullable = false,updatable = false,length = 64)
    private String ownerId;
    /** 权限名称 */
    @Column (nullable = false, length = 32)
    private String name;
    /** 权限显示名 */
    @Column (name = "display_name", nullable = false, length = 128)
    private String displayName;
    /** 权限实现类名 */
    @Column (name = "class_name", nullable = false, length = 64)
    private String className;
    /** 权限类别显示名（classname也是权限类别） */
    @Column (name = "class_display_name", nullable = false, length = 128)
    private String classDisplayName;
    /** 该权限定义的操作 */
    @Column (length = 64)
    private String action;
    /** 排序码 */
    @Column (name = "sort_code")
    private int sortCode;
    /** 权限创建时间 */
    @Column (name = "creation_date", updatable = false)
    private Date creationDate;

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }

    public String getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(String ownerId) {
        this.ownerId = ownerId;
    }

    public int getGroupId()
    {
        return groupId;
    }

    public void setGroupId(Integer groupId)
    {
        this.groupId = groupId;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getDisplayName()
    {
        return displayName;
    }

    public void setDisplayName(String displayName)
    {
        this.displayName = displayName;
    }

    public String getClassName()
    {
        return className;
    }

    public void setClassName(String className)
    {
        this.className = className;
    }

    public String getClassDisplayName()
    {
        return classDisplayName;
    }

    public void setClassDisplayName(String classDisplayName)
    {
        this.classDisplayName = classDisplayName;
    }

    public String getAction()
    {
        return action;
    }

    public void setAction(String action)
    {
        this.action = action;
    }

    public Date getCreationDate()
    {
        return creationDate;
    }

    public void setCreationDate(Date creationDate)
    {
        this.creationDate = creationDate;
    }

    public int getSortCode()
    {
        return sortCode;
    }

    public void setSortCode(int sortCode)
    {
        this.sortCode = sortCode;
    }

    /**
     * 获取权限对象
     *
     * @return 权限对象
     */
    public Permission getPermission()
    {
        //构造权限类，权限实现类必须提供带name参数的构造函数
        Permission p = ReflectUtils.newInstance(getClassName(), new Object[]{getName()}, new Class[]{String.class});
        if (p instanceof ActionPermission) {
            ((ActionPermission)p).setActions(getAction());
        }
        return p;
    }
}
